import json
import logging
from datetime import datetime
from credit_voice_service.dialogue.factory.base_info.strategy.base_info_strategy import BaseInfoStrategy


class BaseInfoStrategy2148(BaseInfoStrategy):

    def get_base_info(self, session_id, user_info):
        # 名字 年龄  优惠券名称 权益 降息幅度 降息剩余天数 拨次 用户阶段
        name = ''
        age = ''
        certificate = ''
        right = '无权益'
        rightCutRate = ''
        cut_rate_day = ''
        call_count = ''
        user_stage = ''
        try:
            if not user_info:
                return {'用户姓名': name, '用户年龄': age, '初始用户权益': right, '券信息': certificate,
                        '降息幅度': rightCutRate, '降息剩余天数': cut_rate_day, 'call次': call_count,
                        '用户阶段': user_stage}
            name = user_info.get('fullName', '')
            sex = user_info.get('sexByChineseWord', '')
            if name and sex:
                name = name + sex
            else:
                name = ''
            age = user_info.get('ageRange', '')
            certificate = ''
            low_interest = False
            coupon = False
            withdrawal = False

            couponDetailJson = user_info.get('couponDetailList', '')
            if couponDetailJson:
                couponDetailList = json.loads(couponDetailJson)
                for couponDetail in couponDetailList:
                    activityName = couponDetail.get('activityName', '')
                    if activityName:
                        if '免息' in activityName and '券' in activityName:
                            certificate = activityName
                            break
                        elif '券' in activityName:
                            certificate = activityName
                            continue

            cutRateTime = user_info.get('cutRateTime', '')
            try:
                if cutRateTime and datetime.strptime(cutRateTime, '%Y-%m-%d'):
                    cutRateTime = datetime.strptime(cutRateTime, '%Y-%m-%d')
                    current_date = datetime.now()
                    days_left = (cutRateTime - current_date).days
                    if days_left >= 0:
                        cutRate = user_info.get('cutRate', '')
                        if cutRate and cutRate == '是':
                            low_interest = True
                            if days_left > 7:
                                cut_rate_day = '降息8天后到期'
                            else:
                                cut_rate_day = '降息{}天到期'.format(days_left)
                        # 降息前
                        beforeCutRate = user_info.get('beforeCutRate', '')
                        # 降息后
                        currentRate = user_info.get('currentRate', '')

                        beforeCutRate = beforeCutRate.replace('-', '')
                        currentRate = currentRate.replace('-', '')

                        if beforeCutRate and currentRate:
                            # 当前费率是这个 同一都是低
                            if '0.00015 ~ 0.00025' == currentRate:
                                rightCutRate = '利息降到最低'
                            # 当前费率
                            if '0.00025 ~ 0.00035' == currentRate and (
                                    '0.00045 ~ 0.00055' == beforeCutRate or '0.00055 ~ 0.00065' == beforeCutRate):
                                rightCutRate = '利息降幅大'
                            if '0.00035 ~ 0.00045' == currentRate and '0.00055 ~ 0.00065' == beforeCutRate:
                                rightCutRate = '利息降幅大'

            except Exception:
                logging.error('日期转换错误')

            couponName = user_info.get('couponName', '')
            if couponName or certificate:
                coupon = True

            try:
                tradeTime = user_info.get('tradeTime', '')
                if tradeTime and datetime.strptime(tradeTime, '%Y-%m-%d'):
                    withdrawal = True
            except Exception:
                logging.error('日期转换错误')

            try:
                batchId = user_info.get('batchId', '')
                if '成长期' in batchId:
                    user_stage = '成长期'
                elif '成熟期' in batchId:
                    user_stage = '成熟期'
            except Exception:
                logging.error('批次获取错误')

            try:
                data = session_id.split('-')
                task_id = data[4]
                if "87" == task_id:
                    call_count = '2-call'
                else:
                    call_count = '1-call'
            except Exception:
                logging.error('获取Call次报错')
            rightList = list()
            if low_interest:
                rightList.append('已获得降息')
            if withdrawal:
                rightList.append('已获得提额')
            if coupon:
                rightList.append('已获得优惠券')
            if not rightList:
                rightList.append('无权益')
            right = ','.join(rightList)
            return {'用户姓名': name, '用户年龄': str(age), '初始用户权益': right, '券信息': certificate,
                    '降息幅度': rightCutRate, '降息剩余天数': cut_rate_day, 'call次': call_count,
                    '用户阶段': user_stage}
        except Exception:
            return {'用户姓名': name, '用户年龄': str(age), '初始用户权益': right, '券信息': certificate,
                    '降息幅度': rightCutRate, '降息剩余天数': cut_rate_day, 'call次': call_count,
                    '用户阶段': user_stage}
